#此文件定义所有的查换包定义处理,用于兼容
#EWX三方库包的下载
#关键变量
#目录或全局属性 EWX_3RD_PKGS: 三方库依赖项信息

#以下两变量是全局使用的内部变量,主要用于存放三方库运行时环境的变量
#可用于Linux和Windows两种平台的环境
#由三方库支撑模块集进行扩展处理 EWX_3RD_Support/Support_*.cmake
SET(EWX_3RD_RUNTIME_DEBUG )
SET(EWX_3RD_RUNTIME_RELEASE )
#==============================================================================
#==============================================================================
#==============================================================================
SET(SUPPORT_3RD_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/EWX_3RD_Support)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${SUPPORT_3RD_ROOT_DIR})


#输入参数 EXE_TARGET_DIRECTORY
#用于绑定文件夹所依赖的三方库信息
MACRO(ewx_3rd_support_pkgs EXE_TARGET_DIRECTORY)
    message(STATUS "--->Find 3rd party packages ${ARGN} for project [${PROJECT_NAME}]...${CURRENT_LIST_DIR}")
    foreach(arg ${ARGN})
        foreach(pkg ${arg})
            string(REGEX MATCH "(.*)-(.*)" pkgNameMatch "${pkg}")
            set_property(GLOBAL PROPERTY ${pkg}_EWX_ACQUIRED TRUE)
            if(NOT pkgNameMatch)
                message(FATAL_ERROR "Third party package: ${pkg} cannot determine package name")
            endif()
            set(pkgBaseName_orig ${CMAKE_MATCH_1})
            
            #追加缓存全局变量
            SET(${pkgBaseName_orig}_FULL_PKG_NAME ${pkg} CACHE INTERNAL "" FORCE)

            string(TOLOWER ${pkgBaseName_orig} pkgBaseName)
            set(pkgVersion ${CMAKE_MATCH_2})
            message(STATUS "\n====ewxdev_find_packages: pkgBaseName=${pkgBaseName} pkgVersion=${pkgVersion}")
            set_property(GLOBAL PROPERTY ${pkg}_BASE_NAME ${pkgBaseName})
            set_property(GLOBAL PROPERTY ${pkg}_VERSION ${pkgVersion})

            set(EWX_3RD_USE_PKG_${pkgBaseName} ${pkg} CACHE STRING "" FORCE)
            mark_as_advanced(FORCE EWX_3RD_USE_PKG_${pkgBaseName})
            
            #这里直接包括三方库的支撑库来确定三方库的依赖信息
            SET(__3RD_MODUEL_FILE ${SUPPORT_3RD_ROOT_DIR}/Support_${pkgBaseName_orig}.cmake)
            SET(__3RD_MODUEL_FILE_lower ${SUPPORT_3RD_ROOT_DIR}/Support_${pkgBaseName}.cmake)
            IF(NOT EXISTS "${__3RD_MODUEL_FILE}" AND NOT EXISTS "${__3RD_MODUEL_FILE_lower}")
                message(FATAL_ERROR "Not Exists Pkg CMake File....${__3RD_MODUEL_FILE}")
            else()
               message(STATUS "====Include Pkg CMake File....${__3RD_MODUEL_FILE}")
               if(EXISTS "${__3RD_MODUEL_FILE}")
                  include(${__3RD_MODUEL_FILE})
               else()
                  include(${__3RD_MODUEL_FILE_lower})
               endif()
                if ("${EXE_TARGET_DIRECTORY}" STREQUAL "")
                    set_property(GLOBAL APPEND PROPERTY EWX_3RD_PKGS ${pkgBaseName})
                else()
                    set_property(DIRECTORY ${EXE_TARGET_DIRECTORY} APPEND 
                        PROPERTY EWX_3RD_PKGS ${pkgBaseName})
                endif()
            ENDIF()
        endforeach()
   endforeach()
ENDMACRO()
#==============================================================================
#链接库处理
macro(ewx_3rd_package_link_libraries_target TARGET)
   foreach(arg ${ARGN})
      foreach(pkg ${arg})
         string(REGEX MATCH "([^-]*).*" pkgNameMatch "${pkg}")
         if(NOT pkgNameMatch)
            message(FATAL_ERROR "Third party package: ${pkg} cannot determine package name")
         endif()
         set(pkgBaseName ${CMAKE_MATCH_1})

         message(STATUS "====Target ${TARGET} Ready Use Package...${pkgBaseName}...")
         if (DEFINED ${pkgBaseName}_LIBRARY)
            message(STATUS "====${pkgBaseName} Use ${pkgBaseName}_LIBRARY Mode...")
            target_link_libraries(${TARGET} 
               ${${pkgBaseName}_LIBRARY})
            #target_link_libraries(${TARGET} 
               #PUBLIC 
               #"$<BUILD_INTERFACE:${${pkgBaseName}_LIBRARY}>")
         else()
            message(STATUS "====${pkgBaseName} Use Normal Mode...")
            if(DEFINED ${pkgBaseName}_LIBRARY_DEBUG)
               target_link_libraries(${TARGET} debug ${${pkgBaseName}_LIBRARY_DEBUG})
            elseif(DEFINED ${pkgBaseName}_LIBRARY_RELEASE)
               target_link_libraries(${TARGET} release ${${pkgBaseName}_LIBRARY_RELEASE})
            else()
               message("Could not find a library definition to link ${TARGET} to ${pkgBaseName}")
            endif()
         endif()
      endforeach()
   endforeach()
endmacro()
#==============================================================================
#生成VS调试文件处理
set(TEMPLATE_DIR "${CMAKE_CURRENT_LIST_DIR}/Templates")


# WARNING : If changes the number of arguments in this function,
# you MUST update the "(${ARGC} GREATER ...)" checks within the macro too.
macro(ewx_3rd_write_vcproj_user EXE_TARGET_DIRECTORY
                        USER_FILE_TEMPLATE
                        VCPROJ_OUT_DIRECTORY
                        ADDITIONAL_PATH_DEBUG
                        ADDITIONAL_PATH_RELEASE
                        ADDITIONAL_ENVIRONMENT_DEBUG
                        ADDITIONAL_ENVIRONMENT_RELEASE)
   SET(options DEBUG)
   SET(oneValueArgs TARGET DEBUGGER_COMMAND DEBUGGER_COMMAND_ARGS)
   SET(multiValueArgs)

   cmake_parse_arguments(_ARGS 
            "${options}" 
            "${oneValueArgs}" 
            "${multiValueArgs}" 
            ${ARGN}
   )
   if (MSVC_IDE)

      IF(_ARGS_DEBUG)
         message(NOTICE "====ewx_3rd_write_vcproj_user...Debug INFOs......")
         message(STATUS "  EXE_TARGET_DIRECTORY: ${EXE_TARGET_DIRECTORY}")
         message(STATUS "  USER_FILE_TEMPLATE: ${USER_FILE_TEMPLATE}")
         message(STATUS "  ADDITIONAL_PATH_DEBUG: ${ADDITIONAL_PATH_DEBUG}")
         message(STATUS "  ADDITIONAL_PATH_RELEASE: ${ADDITIONAL_PATH_RELEASE}")
         message(STATUS "  ADDITIONAL_ENVIRONMENT_DEBUG: ${ADDITIONAL_ENVIRONMENT_DEBUG}")
         message(STATUS "  ADDITIONAL_ENVIRONMENT_RELEASE: ${ADDITIONAL_ENVIRONMENT_RELEASE}")
      ENDIF()

      set(DEBUG_PATH "${ADDITIONAL_PATH_DEBUG}\;%PATH%")
      set(RELEASE_PATH "${ADDITIONAL_PATH_RELEASE}\;%PATH%")

      if ("${EXE_TARGET_DIRECTORY}" STREQUAL "")
         get_property(PKGS GLOBAL PROPERTY EWX_3RD_PKGS)
      else()
         get_property(PKGS DIRECTORY ${EXE_TARGET_DIRECTORY} PROPERTY EWX_3RD_PKGS)
      endif()
      list(REMOVE_DUPLICATES PKGS)

      IF(_ARGS_DEBUG)
         message(NOTICE "====${EXE_TARGET_DIRECTORY} Use [${PKGS}]")
      ENDIF()

      #这里获取所有三方库的运行时目录
      foreach(pkg ${PKGS})
         get_property(${pkg}_BINDIR_DEBUG GLOBAL PROPERTY ${pkg}_BINDIR_DEBUG)
         
         foreach(pkgBinDir ${${pkg}_BINDIR_DEBUG})
            set(DEBUG_PATH "${pkgBinDir}\;${DEBUG_PATH}")
         endforeach()

         get_property(${pkg}_BINDIR_RELEASE GLOBAL PROPERTY ${pkg}_BINDIR_RELEASE)
         #message(STATUS "---------->${pkg}_BINDIR_RELEASE = ${${pkg}_BINDIR_RELEASE}")
         foreach(pkgBinDir ${${pkg}_BINDIR_RELEASE})
            set(RELEASE_PATH "${pkgBinDir}\;${RELEASE_PATH}")
         endforeach()

         if(_ARGS_DEBUG)
            message(STATUS "-----DEBUG: PKG=${pkg}
            BINDIR_DEBUG=${${pkg}_BINDIR_DEBUG}
            BINDIR_RELEASE=${${pkg}_BINDIR_RELEASE}")
         ENDIF()
      endforeach()

      #message(FATAL_ERROR ${DEBUG_PATH})
      #modify by luli:
      IF(NOT "x${PROJECT_WORKING_DIR_DEBUG}" STREQUAL "x")
         SET(DEBUG_PATH "${DEBUG_PATH}\;${PROJECT_WORKING_DIR_DEBUG}")
      ENDIF()
      message(NOTICE "========${PROJECT_NAME} Use write_vcproj_user: DEBUG_PATH =\n  ${DEBUG_PATH}")

      IF(NOT "x${PROJECT_WORKING_DIR_RELEASE}" STREQUAL "x")
         SET(RELEASE_PATH "${RELEASE_PATH}\;${PROJECT_WORKING_DIR_RELEASE}")
      ENDIF()
      message(NOTICE "========${PROJECT_NAME} Use write_vcproj_user: RELEASE_PATH =\n  ${RELEASE_PATH}")
      #RESOURCE_PATH设置不成功先去除，在需要使用的app下的cmake中重新设置 by zhangjiuyang
      set(DEBUG_ENVIRONMENT "${ADDITIONAL_ENVIRONMENT_DEBUG}\nSOURCE_ROOT=${TOOLS_DIRECTORY}/..\nPATH=${DEBUG_PATH}")#\nRESOURCE_PATH=${VTK_DEV_RESOURCES_PATH}
      set(RELEASE_ENVIRONMENT "${ADDITIONAL_ENVIRONMENT_RELEASE}\nSOURCE_ROOT=${TOOLS_DIRECTORY}/..\nPATH=${RELEASE_PATH}")#\nRESOURCE_PATH=${VTK_DEV_RESOURCES_PATH}
      string(REPLACE "/" "\\" DEBUG_ENVIRONMENT ${DEBUG_ENVIRONMENT})
      string(REPLACE "/" "\\" RELEASE_ENVIRONMENT ${RELEASE_ENVIRONMENT})

      if(_ARGS_DEBUGGER_COMMAND)
         set(DEBUGGER_COMMAND "${_ARGS_DEBUGGER_COMMAND}")
         string(REPLACE "/" "\\" DEBUGGER_COMMAND ${DEBUGGER_COMMAND})
      endif()
      if(_ARGS_DEBUGGER_COMMAND_ARGS)
         set(DEBUGGER_COMMAND_ARGS "${_ARGS_DEBUGGER_COMMAND_ARGS}")
         string(REPLACE "/" "\\" DEBUGGER_COMMAND_ARGS ${DEBUGGER_COMMAND_ARGS})
      endif()
      
      set(VCPROJ_OUT_DIR ${PROJECT_BINARY_DIR})
      if (NOT "${VCPROJ_OUT_DIRECTORY}" STREQUAL "")
         set(VCPROJ_OUT_DIR ${VCPROJ_OUT_DIRECTORY})
      endif()

      if(_ARGS_DEBUG)
         message(STATUS "  VCPROJ_OUT_DIR:${VCPROJ_OUT_DIR}")
      endif()
      
      IF(_ARGS_TARGET)
         configure_file(${USER_FILE_TEMPLATE} ${VCPROJ_OUT_DIR}/${_ARGS_TARGET}.vcxproj.user)
      else()
         configure_file(${USER_FILE_TEMPLATE} ${VCPROJ_OUT_DIR}/${PROJECT_NAME}.vcxproj.user)
      ENDIF()
   endif (MSVC_IDE)
endmacro()
#==============================================================================